

jQuery(function ($) {
    var onoff = false;
    var pageOnoff = true;
    $('.works_show ').removeClass('clearfix').addClass('resetInlineBlock');
    $('.search_pic').removeClass('clearfix');
    //一进来就加载已经加入对比框的商品
    if ($.cookie('goods_list') != undefined) {
        // $('#compare_box').show();
        reloadData();
        var localData = getLocalData();
        if (localData == undefined) return;
        for (var i = 0; i < localData.length; i++) {
            var obj = localData[i];
            var id = obj.productID;
            $('[data-id=' + id + ']').find(".contrast").addClass('contrast_se');
        }
    };

    function twbsPagination(total) {
        //分页按钮
      
        Page({
			num:Math.ceil(total / 60),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_b'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    requestGoodsList(page - 1);
                    $('.works_show').html('');
                    $('html,body').animate({scrollTop:$('.s_content ').offset().top-80}, 500);

                }
			}
		});

    }
    requestGoodsList(0);
    function requestGoodsList(page) {
        var params = {
            "Goods[]": {
                "page": page,
                "count": "60",
                "query": 2,
                "Goods": {
                    "@order":"rank-,create_time-",
                    "preview_flag": 1,
                    "GoodsImgs": {
                        "main_flag": 1,
                        "use_flag": 1
                    }
                }
            },
            "total@": "/Goods[]/total"
        };
        send_ajax('post', '/get', params, function callback(res) {
            if (res.code == 200&&res['Goods[]']) {
                var data = res['Goods[]'];
                for (var k = 0; k < data.length; k++) {
                    if (data[k].preview_flag) {
                        if (data[k].evaluate_price == '') {
                            data[k].evaluate_price = '议价';
                        }
                        data[k].evaluate_price = data[k].evaluate_price.split(',');
                        if (data[k].evaluate_price.length > 1 && data[k].evaluate_price[1] == 0) {
                            data[k].evaluate_price = '预估价:' + "￥" + data[k].evaluate_price[0] + '以上';
                        } else if (data[k].evaluate_price.length > 1 && data[k].evaluate_price[1] != 0) {
                            data[k].evaluate_price = '预估价:' + "￥" + data[k].evaluate_price.join('-');
                        };
                        if (data[k].evaluate_price.length == 1) {
                            if (Number(data[k].evaluate_price[0])) {
                                data[k].evaluate_price = '预估价:' + "￥" + data[k].evaluate_price;
                            } else {
                                data[k].evaluate_price = '预估价:' + data[k].evaluate_price;
                            }
                        }
                    }
                };
                if (res.length == 0) {
                    $('.works_show').append('<h3>\u6CA1\u6709\u6570\u636E</h3>');
                } else {
                    $('.s_nav_right').empty();
                    $('.s_nav_right').append('<span>共有<span>' + res.total + '</span> 幅字画</span>\n');
                    for (var i = 0; i < data.length; i++) {
                        var a_goods = data[i];
                        $('.works_show').append('<li class="works_show_item" data-id="' + a_goods.id + '" data-title="' + a_goods.name + '" data-url="' + (undefined == a_goods.GoodsImgs ? "https://pic.90sjimg.com/back_pic/u/00/00/95/36/55e6adaaa0bb2.jpg" : a_goods.GoodsImgs.img) + '" data-price="' + a_goods.goods_price + '" data-txt="' + a_goods.goods_ad_name + '">\n                        <div class="works_show_pic">\n                            <a  target="_blank" href="/goods-' + a_goods.id + '.html"><img src="' + (undefined == a_goods.GoodsImgs ? "https://pic.90sjimg.com/back_pic/u/00/00/95/36/55e6adaaa0bb2.jpg" : isImgHttp(a_goods.GoodsImgs.thumb)) + '" alt="' + (undefined == a_goods.GoodsImgs ? "" : a_goods.GoodsImgs.alt) + '" alt=""></a>\n <i class="g_status goods_status2 ' + (a_goods.preview_flag == 1 ? 'open' : 'hide') + '"></i>\n                                                        <i class="g_status ' + (a_goods.onsale_flag == false ? 'open' : 'hide') + ('"></i>\n                        </div>\n                        <div class="works_show_item_msg">\n                            <div class="works_show_price hide">\n                                <span>' + (a_goods.preview_flag ? a_goods.evaluate_price : '￥' + a_goods.goods_price) + '</span>\n                            </div>\n                            <div class="works_show_pic_introduce">\n                                <p>\n                                    <a href="/goods-' + a_goods.id + '.html" target="_blank"><span class="title">' + a_goods.name + '</span></a>\n                                    <a href="/goods-' + a_goods.id + '.html" target="_blank"><span class="special_txt">' + a_goods.goods_ad_name + '</span></a>\n                                </p>\n\n                            </div>\n    \n                            <ul class="works_show_item_bottom clearfix">\n                                <li class="item contrast_box">\n                                    <span class="contrast"></span>\n                                    <span>\u5BF9\u6BD4</span>\n                                </li>\n                                <li class="item collect_box">\n                                    <i class="collect"></i>\n                                    <span>\u6536\u85CF</span>\n                                </li>\n                                <li class=" noJoin ">\n                                    <i></i>\n                                    <a href="http://114.116.69.140:8888/dialog_1.htm" target="_blank" class="previews attr">\u4F5C\u54C1\u6B23\u8D4F</a>\n                                </li>\n                            </ul>\n                        </div>\n                    </li>'));
                    }
                    //分页按钮
                    if(pageOnoff){
                        twbsPagination(res.total);
                        pageOnoff=false;
                    }
                   
                    //渲染已经加入的对比
                    if ($.cookie('goods_list') != undefined) {
                        // $('#compare_box').show()
                        reloadData();
                        var localData = getLocalData();
                        if (localData == undefined) return;
                        for (var i = 0; i < localData.length; i++) {
                            var obj = localData[i];
                            var id = obj.productID;
                            $('[data-id=' + id + ']').find(".contrast").addClass('contrast_se');
                        }
                    };
                    isStore(); //是否已经收藏过的商品
                }
            }
        });
    };
    //是否已经收藏过的商品
    function isStore() {
        var user = JSON.parse(localStorage.getItem('user_info')) || {};
        var id = user.id;
        if (!id) {
            return;
        }
        var element = $('.works_show_item');
        var goodsIds = [];
        for (var k = 0; k < element.length; k++) {
            goodsIds.push($(element[k]).attr('data-id'));
        };
        var param = {
            "GoodsCollect[]": {
                "GoodsCollect": {
                    "goods_id{}": goodsIds,
                    "user_id": id
                }
            }
        };
        send_ajax('post', '/get', param, function callback(result) {
            if (result.code == 200 && result['GoodsCollect[]'] != undefined) {
                var data = result['GoodsCollect[]'];
                for (var j = 0; j < data.length; j++) {
                    $('[data-id=' + data[j].goods_id + ']').find(".collect").addClass('select_collect');
                }
            }
        });
    };
    //判断是不是数字
    function Number(val) {
        if (parseFloat(val).toString() == "NaN") {
            return false;
        } else {
            return true;
        }
    };
});